package com.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.springcloud.service.XxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class XxController {
    @Resource
    private XxService service;

    @RequestMapping(value = "/hystrix/ok")
    @ResponseBody
    public String HyStrix_ok(){
        return service.HyStrix_ok();
    }
    @RequestMapping(value = "/hystrix/no/ok")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "TimeOutHandler",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "1500")
    })
    public String HyStrix_no_ok(){
        return service.HyStrix_no_ok();
    }
    //兜底方案 1.5秒不返回 立马进行兜底方案 防止服务器down机
    public String TimeOutHandler(){
        return "我是服务器9001,现在请求报错了，请检查8001服务器或者检查9001服务器";
    }
    @RequestMapping(value = "/hystrix/zero")
    @ResponseBody
    public String Hystrix_zero(){
        return service.Hystrix_zero();
    }
    @RequestMapping(value = "/hystrix/{id}")
    @ResponseBody
    public String Hystrix(@PathVariable("id") Integer id){
        return service.Hystrix(id);
    }

}
